<?php

namespace app\admin\model;

use think\Model;


class MediaLog extends Model
{

    

    

    // 表名
    protected $name = 'media_look_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'lasttime_text'
    ];
    

    



    public function getLasttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lasttime']) ? $data['lasttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setLasttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public static function look($para,$uid)
    {
        $where = [
            'media_id'    => $para['id'],
            'user_id'   => $uid
        ];
        $time = time();
        $row = self::where($where)->find();
        if ($row) {
            $row->times = $row->times+1;
            $row->lasttime = $time;
            $row->save();
            return true;
        }
        $data = [
            'media_id' => $para['id'],
            'user_id' => $uid,
            'times' => 1,
            'createtime' => $time,
            'lasttime' => $time
        ];
        self::insert($data);
        return true;
    }
}
